package com.source.multithread.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;

/**
 * @Author wengqy
 * @Date 2021/1/8
 */
public class AntotaionTest {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
     //   Class<Student> stuCla = Student.class;
        Class<Student> stuCla = (Class<Student>) Class.forName("com.source.multithread.annotation.Student");
        QuanYuan annotation = stuCla.getAnnotation(QuanYuan.class);
      //  System.out.println(annotation +"   #   "+annotation.value());


        Annotation[] annotations = stuCla.getAnnotations();
        for (Annotation an  : annotations) {
            if(an instanceof QuanYuan) {
                System.out.println( ((QuanYuan) an).value());
            }

            System.out.println(an.annotationType()+"--#--"+an.annotationType().getName()+"***"+an.annotationType().getTypeParameters() );
        }


        System.out.println("--------------------------------");
        Class<Student> stu = (Class<Student>) Class.forName("com.source.multithread.annotation.Student");
        /*Field name = stu.getDeclaredField("name");
        FieldSourceAnta a  = name.getAnnotation(FieldSourceAnta.class);
        System.out.println(a.maxLength());
        System.out.println(a.value());*/
        Field[] fields = stu.getDeclaredFields();
        for (Field field : fields) {
            Annotation[] ans = field.getAnnotations();
            for (Annotation an : ans) {
                String value = ((FieldSourceAnta) an).value();
                int length = ((FieldSourceAnta) an).maxLength();
                System.out.println(value +"    #      "+length);
            }
        }
        System.out.println("..................888888888888.................");


        Class<Student> stuu = (Class<Student>) Class.forName("com.source.multithread.annotation.Student");
        Student student = stuu.newInstance();
        Method mm = stuu.getMethod("studentStu");
        Class<?> returnType = mm.getReturnType();
        System.out.println(returnType.getName());
        System.out.println(returnType);

        Student invoke = (Student) mm.invoke(student, null);
        System.out.println(invoke);
        System.out.println("..................888888888888.................");

        Constructor<Student> constructor = stuu.getConstructor( int.class, String.class, int.class);
        constructor.setAccessible(true);
        Student student1 = constructor.newInstance(3, "333", 5);
        System.out.println(student1);

        System.out.println(constructor);
       /* Method studentStr = stuu.getDeclaredMethod("studentStr", String.class);
        studentStr.setAccessible(true);
        String  res = (String) studentStr.invoke(student, "5566");
        System.out.println(res);*/




    }
}


